package com.mzh.friend.service.question.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.aliyun.tea.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mzh.common.core.domain.TableDataInfo;
import com.mzh.friend.domain.question.Question;
import com.mzh.friend.domain.question.dto.QuestionQueryDTO;
import com.mzh.friend.domain.question.es.QuestionES;
import com.mzh.friend.domain.question.vo.QuestionVO;
import com.mzh.friend.domain.question.vo.QuestionDetailVO;
import com.mzh.friend.elasticsearch.QuestionRepository;
import com.mzh.friend.manager.QuestionCacheMapper;
import com.mzh.friend.mapper.question.QuestionMapper;
import com.mzh.friend.service.question.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionCacheMapper questionCacheMapper;

    @Override
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
        long count = questionRepository.count();
        if (count<=0){
            refreshQuestion();
        }
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");      //根据创建时间倒序
        Pageable pageable = PageRequest.of(questionQueryDTO.getPageNum()-1,questionQueryDTO.getPageSize(),sort);    //分页
        Integer difficulty = questionQueryDTO.getDifficulty();
        String keyword = questionQueryDTO.getKeyword();
        Page<QuestionES> questionESPage;
        if(difficulty==null && StringUtils.isEmpty(keyword)){
            questionESPage = questionRepository.findAll(pageable);      //查询所有
        } else if (StringUtils.isEmpty(keyword)){
            questionESPage = questionRepository.findQuestionByDifficulty(difficulty,pageable);    //根据难度查询
        } else if (difficulty==null){
            questionESPage = questionRepository.findByTitleOrContent(keyword,keyword,pageable);
        }else {
            questionESPage = questionRepository.findByTitleOrContentAndDifficulty(keyword,keyword,difficulty,pageable);
        }
        long total = questionESPage.getTotalElements();
        if (total ==0){
            return TableDataInfo.empty();
        }
        List<QuestionES> questionESList = questionESPage.getContent();          //获取分页数据
        List<QuestionVO> questionList = BeanUtil.copyToList(questionESList,QuestionVO.class);
        return TableDataInfo.success(questionList,total);
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        QuestionES questionES = questionRepository.findById(questionId).orElse(null);
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        if(questionES!=null){
            BeanUtil.copyProperties(questionES,questionDetailVO);
            return questionDetailVO;
        }
        Question question=questionMapper.selectById(questionId);
        if(question==null){
            return null;
        }
        refreshQuestion();
        BeanUtil.copyProperties(question,questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public String preQuestion(Long questionId) {
        Long listSize = questionCacheMapper.getListSize();
        if(listSize==null || listSize==0){
            questionCacheMapper.refreshCache();
        }
        return questionCacheMapper.preQuestion(questionId).toString();
    }

    @Override
    public String nextQuestion(Long questionId) {
        Long listSize = questionCacheMapper.getListSize();
        if(listSize==null || listSize==0){
            questionCacheMapper.refreshCache();
        }
        return questionCacheMapper.nextQuestion(questionId).toString();
    }

    private void refreshQuestion() {
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>());
        if(CollectionUtil.isEmpty(questionList)){
            return;
        }
        List<QuestionES> questionES = BeanUtil.copyToList(questionList, QuestionES.class);
        questionRepository.saveAll(questionES);
    }
}
